//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmHestonHullWhiteOp.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/Meshers/FdmMesher.h>
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/Processes/HullWhiteProcess.h>
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpComposite.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Methods::Finitedifferences::Meshers;
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::CFdmHestonHullWhiteOp (Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Processes::IHestonProcess^ hestonProcess, Cephei::QL::Processes::IHullWhiteProcess^ hwProcess, Double equityShortRateCorrelation) : CFdmLinearOpComposite(CFdmHestonHullWhiteOp::typeid)
{
    CFdmMesher^ _Cmesher;
    CHestonProcess^ _ChestonProcess;
    CHullWhiteProcess^ _ChwProcess;
    try
    {
#ifdef HANDLE
        _phFdmHestonHullWhiteOp = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ChestonProcess = safe_cast<CHestonProcess^> (hestonProcess);
        _ChestonProcess->Lock();
        boost::shared_ptr<QuantLib::HestonProcess>& _hestonProcess = static_cast<boost::shared_ptr<QuantLib::HestonProcess>&> (_ChestonProcess->GetShared ()); 
        _ChwProcess = safe_cast<CHullWhiteProcess^> (hwProcess);
        _ChwProcess->Lock();
        boost::shared_ptr<QuantLib::HullWhiteProcess>& _hwProcess = static_cast<boost::shared_ptr<QuantLib::HullWhiteProcess>&> (_ChwProcess->GetShared ()); 
        QuantLib::Real _equityShortRateCorrelation = (QuantLib::Real)ValueHelper::Convert (equityShortRateCorrelation); //d
        _ppFdmHestonHullWhiteOp = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteOp> (new QuantLib::FdmHestonHullWhiteOp ( _mesher,  _hestonProcess,  _hwProcess,  _equityShortRateCorrelation ));
        SetFdmLinearOpComposite (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonHullWhiteOp));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_ChestonProcess != nullptr) _ChestonProcess->Unlock();
        if (_ChwProcess != nullptr) _ChwProcess->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::CFdmHestonHullWhiteOp (boost::shared_ptr<QuantLib::FdmHestonHullWhiteOp>& childNative, Object^ owner) : CFdmLinearOpComposite(CFdmHestonHullWhiteOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonHullWhiteOp = NULL;
#endif
	_ppFdmHestonHullWhiteOp = &childNative;
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonHullWhiteOp));
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::CFdmHestonHullWhiteOp (QuantLib::FdmHestonHullWhiteOp& childNative, Object^ owner) : CFdmLinearOpComposite(CFdmHestonHullWhiteOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonHullWhiteOp = NULL;
#endif
	_ppFdmHestonHullWhiteOp = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteOp> (&childNative);
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonHullWhiteOp));
    _FdmHestonHullWhiteOpOwner = owner;
    _FdmLinearOpCompositeOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::CFdmHestonHullWhiteOp (CFdmHestonHullWhiteOp^ copy) : CFdmLinearOpComposite(CFdmHestonHullWhiteOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonHullWhiteOp = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHestonHullWhiteOp = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteOp> (copy->GetShared());
        _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonHullWhiteOp));
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::CFdmHestonHullWhiteOp (PLATFORM::Type^ t) : CFdmLinearOpComposite(CFdmHestonHullWhiteOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonHullWhiteOp = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHestonHullWhiteOp::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::CFdmHestonHullWhiteOp (QuantLib::Handle<QuantLib::FdmHestonHullWhiteOp>& childNative, Object^ owner)  : CFdmLinearOpComposite(CFdmHestonHullWhiteOp::typeid)
{
	_phFdmHestonHullWhiteOp = &childNative;
	_ppFdmHestonHullWhiteOp = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonHullWhiteOp));
    _FdmHestonHullWhiteOpOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::CFdmHestonHullWhiteOp (QuantLib::Handle<QuantLib::FdmHestonHullWhiteOp> childNative)  : CFdmLinearOpComposite(CFdmHestonHullWhiteOp::typeid)
{
	_phFdmHestonHullWhiteOp = &childNative;
	_ppFdmHestonHullWhiteOp = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteOp>>(childNative.currentLink());
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonHullWhiteOp));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::CFdmHestonHullWhiteOp (QuantLib::FdmHestonHullWhiteOp childNative)  : CFdmLinearOpComposite(CFdmHestonHullWhiteOp::typeid)
{
#ifdef HANDLE
	_phFdmHestonHullWhiteOp = NULL;
#endif
	_ppFdmHestonHullWhiteOp = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteOp> (new QuantLib::FdmHestonHullWhiteOp (childNative));
    _ppFdmLinearOpComposite = new boost::shared_ptr<QuantLib::FdmLinearOpComposite> (boost::dynamic_pointer_cast<QuantLib::FdmLinearOpComposite> (*_ppFdmHestonHullWhiteOp));
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::~CFdmHestonHullWhiteOp ()
{
    if (_ppFdmHestonHullWhiteOp != NULL)
    {
	    delete _ppFdmHestonHullWhiteOp;
        _ppFdmHestonHullWhiteOp = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::!CFdmHestonHullWhiteOp ()
{
    if (_ppFdmHestonHullWhiteOp != NULL)
    {
	    delete _ppFdmHestonHullWhiteOp;
    }
}
QuantLib::FdmHestonHullWhiteOp& Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::GetReference ()
{
    if (_ppFdmHestonHullWhiteOp == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmHestonHullWhiteOp;
}
boost::shared_ptr<QuantLib::FdmHestonHullWhiteOp>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::GetShared ()
{
    if (_ppFdmHestonHullWhiteOp == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmHestonHullWhiteOp;
}
QuantLib::FdmHestonHullWhiteOp* Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::GetPointer ()
{
    if (_ppFdmHestonHullWhiteOp == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmHestonHullWhiteOp;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHestonHullWhiteOp>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::GetHandle ()
{
	if (_phFdmHestonHullWhiteOp == NULL)
	{
		_phFdmHestonHullWhiteOp = new Handle<QuantLib::FdmHestonHullWhiteOp> (*_ppFdmHestonHullWhiteOp);
	}
	return *_phFdmHestonHullWhiteOp;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::HasNative () 
{
	return (_ppFdmHestonHullWhiteOp != NULL);
}

Cephei::QL::Methods::Finitedifferences::Operators::IFdmHestonHullWhiteOp^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1); //a
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2); //a
    	(*_ppFdmHestonHullWhiteOp)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmHestonHullWhiteOp)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Operators::IFdmHestonHullWhiteOp^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmHestonHullWhiteOp_Factory::Create (Cephei::QL::Methods::Finitedifferences::Meshers::IFdmMesher^ mesher, Cephei::QL::Processes::IHestonProcess^ hestonProcess, Cephei::QL::Processes::IHullWhiteProcess^ hwProcess, Double equityShortRateCorrelation)
{
    return REFNEW CFdmHestonHullWhiteOp ( mesher,  hestonProcess,  hwProcess,  equityShortRateCorrelation);
}
